/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_RTI_PARTICIPANT_MONITOR_HPP_
#define _DAL_RTI_PARTICIPANT_MONITOR_HPP_

#include <iostream>
#include <atomic>
#include <set>
#include <thread>
#include <chrono>
#include <string>
#include "ndds/ndds_cpp.h"
#include "dal/ret_type.h"
#include "dal/guid.h"
#include "dal/domain_participant.h"
#include "dal/participant_monitor.h"
#include "dal_rticonnextdds/converter/GuidConverter.hpp"
#include "dal_rticonnextdds/entity_impl/DalDomainParticipantImpl.hpp"

struct rti_participant_guid_num_s {
    uint64_t first_half = 0;
    uint64_t second_half = 0;
    void copy_from_rti(DDS_InstanceHandle_t &r_ins) {
        memcpy(&this->first_half, r_ins.keyHash.value, 8);
        memcpy(&this->second_half, r_ins.keyHash.value + 8, 8);
    }
    bool operator<(const rti_participant_guid_num_s &x) const {
        if (first_half < x.first_half) return true;
        if (first_half > x.first_half) return false;
        return second_half < x.second_half;
    }
};

struct rti_participant_discovery_info_s {
    uint32_t born_time = 0;
    uint32_t meta = 0;  // 0 normal, 1 monitor.
};

uint32_t rti_monitor_timestamp();

void rti_participant_monitor(DalDomainParticipantImpl *d_participant_impl);

#endif  // _DAL_RTI_PARTICIPANT_MONITOR_HPP_